package com.example.daxiguaforharmony.game.core.game;

import com.example.daxiguaforharmony.game.core.base.DisplayObjectContainer;
import com.example.daxiguaforharmony.game.core.base.ITouchHandler;
import com.example.daxiguaforharmony.game.core.tween.Tween;
import com.example.daxiguaforharmony.game.core.utils.Callback;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.List;

public class SceneBase {
    protected float _scaleFactor = 1f;
    protected float _sceneWidth = 750;
    protected float _sceneHeight = 1334;
    protected DisplayObjectContainer _container = null;
    protected List<GameObject> _gameObjects = null;
    protected List<ITouchHandler> _touchEventsHandler = null;
    private List<Callback.UpdateCallback> _updateCallbacks = null;
    private List<Callback.UpdateCallback> _preDelUpdateCallbacks = null;
    public SceneBase(float sceneWidth, float sceneHeight) {
        this._sceneWidth = sceneWidth;
        this._sceneHeight = sceneHeight;
        this._scaleFactor = sceneWidth / 750f;
        this._container = new DisplayObjectContainer();
        this._gameObjects = new ArrayList<GameObject>();
        this._touchEventsHandler = new ArrayList<ITouchHandler>();
        this._updateCallbacks = new ArrayList<Callback.UpdateCallback>();
        this._preDelUpdateCallbacks = new ArrayList<Callback.UpdateCallback>();
        Tween.setScene(this);
    }
    public void addUpdateCallback(Callback.UpdateCallback callback) {
        synchronized (this._updateCallbacks) {
            this._updateCallbacks.add(callback);
        }
    }
    public void removeUpdateCallback(Callback.UpdateCallback callback) {
        synchronized (this._updateCallbacks) {
            this._updateCallbacks.remove(callback);
        }
    }
    public void removeUpdateCallbackByTarget(Object target) {
        synchronized (this._updateCallbacks) {
            this._preDelUpdateCallbacks.clear();
            for (int i = 0; i < this._updateCallbacks.size(); i++) {
                Callback.UpdateCallback callback = this._updateCallbacks.get(i);
                if (callback != null && callback.target == target) {
                    this._preDelUpdateCallbacks.add(callback);
                }
            }
            for (int i = 0; i < this._preDelUpdateCallbacks.size(); i++) {
                Callback.UpdateCallback callback = this._preDelUpdateCallbacks.get(i);
                this._updateCallbacks.remove(callback);
            }
            this._preDelUpdateCallbacks.clear();
        }
    }
    public void update(float dt) {
        synchronized (this._updateCallbacks) {
            for (int i = 0; i < this._updateCallbacks.size(); i++) {
                Callback.UpdateCallback callback = this._updateCallbacks.get(i);
                if (callback != null) callback.update(dt);
            }
        }
    }
    public boolean onTouchEvent(TouchEvent touchEvent) {
        System.out.println("onTouchEvent 开始进入=======");
        for (int i = this._touchEventsHandler.size() - 1; i >= 0; i--) {
            ITouchHandler handler = this._touchEventsHandler.get(i);
            if (handler == null) continue;
            boolean flag = handler.onTouchEvent(touchEvent);
            if (flag == true) return true;
        }
        return false;
    }
    public float getSceneWidth() {
        return this._sceneWidth;
    }
    public float getSceneHeight() {
        return this._sceneHeight;
    }
    public float getScaleFactor() {
        return this._scaleFactor;
    }
    public DisplayObjectContainer getDisplayContainer() {
        return this._container;
    }
    public List<GameObject> getGameObjects() { return this._gameObjects; }
    public void addGameObject(GameObject gameObject) {
        this._gameObjects.add(gameObject);
    }
    public void removeGameObject(GameObject gameObject) {
        this._gameObjects.remove(gameObject);
    }
    public void addTouchHandler(ITouchHandler handler) {
        this._touchEventsHandler.add(handler);
    }
    public void removeTouchHandler(ITouchHandler handler) {
        this._touchEventsHandler.remove(handler);
    }
}
